import com.adobe.cairngorm.control.CairngormEventDispatcher;

import mx.formatters.DateFormatter;

import net.optix.ip.events.PublishInventionEvent;
import net.optix.ip.events.UpdateInventionEvent;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.InventionVO;

import views.ViewInstance;

[Bindable]
private var model:ModelLocator	=	ModelLocator.getInstance();

public var _statusInvention:int;
public var _invention:InventionVO;

public function set statusInvention(value:int):void
{
	_statusInvention	=	value;
	switch(value)
	{
		case 0:
			this.label	=	"";
			break;
		case 1:
			this.label	=	"File Provisional";
			break;
		case 2:
			this.label	=	"File Non-provisional";
			break;
		case 3:
			this.label	=	"Publish";
			
			break;
		case 4:
			this.label	=	"Preparing Application";
			break;
		case 5:
			this.label	=	"Inventor Review";
			break;
		case 6:
			this.label	=	"Filed";
			break;
		case 7:
			this.label	=	"Examination";
			break;
		case 8:
			this.label	=	"Allowance";
			break;
		case 9:
			this.label	=	"Issue";
			break;
		case 10:
			this.label	=	"Abandon";
			break;
	}
}

public function get statusInvention():int
{
	return _statusInvention;
}

public function get invention():InventionVO
{
	return _invention;
}

public function set invention(value:InventionVO):void
{
	_invention	=	value;
	updateVisible();
	this.enabled	=	true;
	
	var status:int	=	value.status;
	
	if(status == 1 || status == 2 || status == 3 )
	{
		if(this._statusInvention == 1 
			|| this._statusInvention == 2
			|| this._statusInvention == 3 )
		{
			this.enabled	=	false;	
		}
	}
	
	if(status == 6)
	{
		if(this._statusInvention == 5 || this._statusInvention == 4 || this._statusInvention == 6)
		{
			this.enabled	=	false;
		}
	}
	
	if(status>= 7 && status <= 10)
	{
		if(this._statusInvention == 4 || this._statusInvention == 5)
		{
			this.enabled	=	false;
		}
	}
}

private function updateVisible():void
{
	this.visible	=	true;
	switch(model.role)
	{
		case 0:
			this.visible	=	false;
			break;
		case 1:
			if(_invention.clientID	== model.client.clientID)
			{
				this.visible	=	true;
			}
			else
			{
				this.visible	=	false;	
			}
			break;
		case 2:
			if(_invention.inventorID	== model.inventor.inventorID)
			{
				this.visible	=	true;
			}
			else
			{
				this.visible	=	false;
			}
			break;
		case 3:
			if(_invention.firmID	!= model.firm.firmID || _invention.bConfirm != 1)
			{
				this.visible	=	false;
			}
			else
			{
				this.visible	=	true;
			}
			break;
		case 4:
			if(_invention.practitionerID	!= model.practitioner.practitionerID || _invention.bConfirm != 1)
			{
				this.visible	=	false;
			}
			else
			{
				this.visible	=	true;
			}
			break;
		case 5:
			this.visible	=	false;
			break;	
	}
}

private function updateInventionHandle():void
{
	var dateFormat:DateFormatter	=	new DateFormatter();
	dateFormat.formatString			=	"YYYY/MM/DD";
	
	var date:Date	=	new Date(Date.parse(dateFormat.format(_invention.published)));
	trace(date.getFullYear()+"  year "+date.getUTCMonth()+"  month  "+date.getUTCDate()+"  day  ");
	var today:Date	=	new Date();
	
	var date1:Date	=	new Date(date.getFullYear()+1,date.getUTCMonth(),date.getUTCDate(),0,0,0,0);
	
	trace(date1.getFullYear()+"  year "+date1.getUTCMonth()+"  month  "+date1.getUTCDate()+"  day  ");
	
	if(date1.getTime() < today.getTime() && _statusInvention <= 2 && _invention.published != '')
	{
		ViewInstance.showMessage("","Can't change status");
	}
	else
	{
		
	
	
	
	
	model.bSubmit	=	false;
	if(this._statusInvention == 6)
	{
		model.actionView	=	0;
		model.actionView	=	ViewInstance.AddFieldAction;
	}
	else if(this._statusInvention == 9)
	{
		model.actionView	=	0;
		model.actionView	=	ViewInstance.GetDate;
		
	}
	else
	{
		if(this._statusInvention != 3)
		{
			var vo:InventionVO	=	model.currentInvention;
			vo.status	=	this._statusInvention;
			model.bSubmit	=	false;
			var event:UpdateInventionEvent	=	new UpdateInventionEvent(vo);
			CairngormEventDispatcher.getInstance().dispatchEvent(event);
		}
		else
		{
			
			var event1:PublishInventionEvent	=	
				new PublishInventionEvent(model.currentInvention.inventionID);
			CairngormEventDispatcher.getInstance().dispatchEvent(event1);
		}
	}
	
	}
}